﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCDanhSachHoaDonNhapHang : UserControl
    {
        String id_HoaDonNhapHang = null;
        String idnhanvien;
        public UCDanhSachHoaDonNhapHang(String idtoancuc)
        {
            InitializeComponent();
            this.idnhanvien = idtoancuc;
            showHoaDonNhapHang();
        }
        /// <summary>
        /// Hiển thị hóa đơn nhập hàng
        /// </summary>
        void showHoaDonNhapHang()
        {
            HoaDonNhapBUO hoadonnhap = new HoaDonNhapBUO();
            dataGridView1.DataSource = hoadonnhap.GetAllHDNhap();
        }
        private void rdTimTheoMaDDH_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoNCC.Enabled = false;
            txtTimTheoNCC.Text = "";
            dateTimePicker1.Enabled = false;

        }

        private void rdTimTheoNCC_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMaHD.Enabled = false;
            txtTimTheoMaHD.Text = "";
            txtTimTheoNCC.Enabled = true;
            dateTimePicker1.Enabled = false;
        }

        private void rdTimTheoNgay_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMaHD.Enabled = false;
            txtTimTheoMaHD.Text = "";
            txtTimTheoNCC.Enabled = false;
            txtTimTheoNCC.Text = "";
            dateTimePicker1.Enabled = true;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            HoaDonNhapBUO hoadonnhap = new HoaDonNhapBUO();
            if (rdTimTheoMaDDH.Checked==true)
            {
                
                dataGridView1.DataSource = hoadonnhap.SearchByMaHoaDonNhap(txtTimTheoMaHD.Text.Trim());
            }

            if (rdTimTheoNCC.Checked==true)
            {
                dataGridView1.DataSource = hoadonnhap.SearchByNhaCungCap(txtTimTheoNCC.Text.Trim());
            }

            if (rdTimTheoNgay.Checked==true)
            {
                dataGridView1.DataSource = hoadonnhap.SearchByNgayThang(DateTime.Parse(dateTimePicker1.Text));
            }
        }

        private void txtTimTheoMaHD_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void txtTimTheoMaHD_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                if (txtTimTheoMaHD.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Chưa nhập thông tin cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    HoaDonNhapBUO hoadonnhap = new HoaDonNhapBUO();
                    dataGridView1.DataSource = hoadonnhap.SearchByMaHoaDonNhap(txtTimTheoMaHD.Text.Trim());
                }
                
            }
            
        }

        private void txtTimTheoNCC_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void txtTimTheoNCC_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                if (txtTimTheoNCC.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Chưa nhập thông tin cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    HoaDonNhapBUO hoadonnhap = new HoaDonNhapBUO();
                    dataGridView1.DataSource = hoadonnhap.SearchByNhaCungCap(txtTimTheoNCC.Text.Trim());
                }
            }
            
        }

        private void txtTimTheoMaHD_TextChanged(object sender, EventArgs e)
        {
            HoaDonNhapBUO hoadonnhap = new HoaDonNhapBUO();
            dataGridView1.DataSource = hoadonnhap.SearchByMaHoaDonNhap(txtTimTheoMaHD.Text.Trim());
        }
        /// <summary>
        /// Tìm theo tên nhà cung cấp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtTimTheoNCC_TextChanged(object sender, EventArgs e)
        {
            HoaDonNhapBUO hoadonnhap = new HoaDonNhapBUO();
            dataGridView1.DataSource = hoadonnhap.SearchByNhaCungCap(txtTimTheoNCC.Text.Trim());
        }

        private void thêmHóaĐơnNhậpHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form = this.ParentForm;
            UCThemHoaDonNhapHang ucthemhdnhap = new UCThemHoaDonNhapHang(idnhanvien);
            ControlAccess.ShowConTrol(form, ucthemhdnhap, "grbHienThiChucNang");
        }
        /// <summary>
        /// Lấy id_HoaDonNhapHang
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        id_HoaDonNhapHang = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void xóaHóaĐơnNhậpHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HoaDonNhapBUO hdbanbuo = new HoaDonNhapBUO();
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (hdbanbuo.Delete(id_HoaDonNhapHang))
                {
                    MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                showHoaDonNhapHang();
            }
        }

        private void sửaHóaĐơnNhậpHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form = this.ParentForm;
            UCSuaHoaDonNhapHang ucSuaHDNhap = new UCSuaHoaDonNhapHang(id_HoaDonNhapHang);
            ControlAccess.ShowConTrol(form, ucSuaHDNhap, "grbHienThiChucNang");
        }
    }
}
